﻿using System.Collections.Generic;
using System.Linq;
using Firestarter.Test.Base;
using FireStarterModeller.BO;
using Habanero.BO;
using NUnit.Framework;

namespace FireStarterModeller.Test.BO
{
    [TestFixture]
    public class TestOrderedCollectionController
    {
        [SetUp]
        public void SetupTest()
        {
            //Runs every time that any testmethod is executed
            TestUtilsDomain.SetupTest();
            BORegistry.DataAccessor = new DataAccessorInMemory();
        }

        [TestFixtureSetUp]
        public void TestFixtureSetup()
        {
            //Code that is executed before any test is run in this class. If multiple tests
            // are executed then it will still only be called once.
            TestUtilsDomain.SetupFixture();
        }

        [Test]
        public void Test_Construct()
        {
            //---------------Set up test pack-------------------
            BusinessObjectCollection<DMProperty> collection = new BusinessObjectCollection<DMProperty>();
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            OrderedCollectionController<DMProperty> controller = new OrderedCollectionController<DMProperty>(collection);
            //---------------Test Result -----------------------
            Assert.AreSame(collection, controller.Collection);
        }

        [Test]
        public void Test_WhenCreateFirstItem_ShouldAddAsFirstOrdinal()
        {
            //---------------Set up test pack-------------------
            BusinessObjectCollection<DMProperty> collection = new BusinessObjectCollection<DMProperty>();
            OrderedCollectionController<DMProperty> controller = new OrderedCollectionController<DMProperty>(collection);
            //---------------Assert Preconditions --------------
            Assert.AreEqual(0, collection.Count);
            //---------------Execute Test ----------------------
            DMProperty item = collection.CreateBusinessObject();
            //---------------Test Result ----------------------
            Assert.AreEqual(1, collection.Count);
            Assert.AreSame(item, collection[0]);
            Assert.AreEqual(1, item.OrdinalPosition);
        }

        [Test]
        public void Test_WhenCreateSecondItem_ShouldAddAsSecondOrdinal()
        {
            //---------------Set up test pack-------------------
            BusinessObjectCollection<DMProperty> collection = new BusinessObjectCollection<DMProperty>();
            OrderedCollectionController<DMProperty> controller = new OrderedCollectionController<DMProperty>(collection);
            DMProperty item1 = collection.CreateBusinessObject();
            //---------------Assert Preconditions --------------
            Assert.AreEqual(1, collection.Count);
            Assert.AreSame(item1, collection[0]);
            Assert.AreEqual(1, item1.OrdinalPosition);
            //---------------Execute Test ----------------------
            DMProperty item2 = collection.CreateBusinessObject();
            //---------------Test Result ----------------------
            Assert.AreEqual(2, collection.Count);
            Assert.AreSame(item1, collection[0]);
            Assert.AreEqual(1, item1.OrdinalPosition);
            Assert.AreSame(item2, collection[1]);
            Assert.AreEqual(2, item2.OrdinalPosition);
        }

        [Test]
        public void Test_WhenCreateSecondItem_WithNoOrdinalsSet_ShouldAddAsFirstOrdinal()
        {
            //---------------Set up test pack-------------------
            BusinessObjectCollection<DMProperty> collection = new BusinessObjectCollection<DMProperty>();
            OrderedCollectionController<DMProperty> controller = new OrderedCollectionController<DMProperty>(collection);
            DMProperty item1 = collection.CreateBusinessObject();
            item1.OrdinalPosition = null;
            //---------------Assert Preconditions --------------
            Assert.AreEqual(1, collection.Count);
            Assert.AreSame(item1, collection[0]);
            Assert.IsNull(item1.OrdinalPosition);
            //---------------Execute Test ----------------------
            DMProperty item2 = collection.CreateBusinessObject();
            //---------------Test Result ----------------------
            Assert.AreEqual(2, collection.Count);
            Assert.AreSame(item1, collection[0]);
            Assert.IsNull(item1.OrdinalPosition);
            Assert.AreSame(item2, collection[1]);
            Assert.AreEqual(1, item2.OrdinalPosition);
        }

        [Test]
        public void Test_WhenCreateNewItem_WithCustomOrdinalsSet_ShouldAddAsNextOrdinal()
        {
            //---------------Set up test pack-------------------
            BusinessObjectCollection<DMProperty> collection = new BusinessObjectCollection<DMProperty>();
            OrderedCollectionController<DMProperty> controller = new OrderedCollectionController<DMProperty>(collection);
            DMProperty item1 = collection.CreateBusinessObject();
            item1.OrdinalPosition = 123;
            DMProperty item2 = collection.CreateBusinessObject();
            item2.OrdinalPosition = 135;
            //---------------Assert Preconditions --------------
            Assert.AreEqual(2, collection.Count);
            //---------------Execute Test ----------------------
            DMProperty item3 = collection.CreateBusinessObject();
            //---------------Test Result ----------------------
            Assert.AreEqual(3, collection.Count);
            List<DMProperty> list = GetSortedList(collection);
            Assert.AreSame(item1, list[0]);
            Assert.AreEqual(123, item1.OrdinalPosition);
            Assert.AreSame(item2, list[1]);
            Assert.AreEqual(135, item2.OrdinalPosition);
            Assert.AreSame(item3, list[2]);
            Assert.AreEqual(136, item3.OrdinalPosition);
        }

        [Test]
        public void Test_WhenCreateNewItem_WithCustomOrdinalsSetAndUnordered_ShouldAddAsNextOrdinal()
        {
            //---------------Set up test pack-------------------
            BusinessObjectCollection<DMProperty> collection = new BusinessObjectCollection<DMProperty>();
            OrderedCollectionController<DMProperty> controller = new OrderedCollectionController<DMProperty>(collection);
            DMProperty item1 = collection.CreateBusinessObject();
            item1.OrdinalPosition = 135;
            DMProperty item2 = collection.CreateBusinessObject();
            item2.OrdinalPosition = 123;
            //---------------Assert Preconditions --------------
            Assert.AreEqual(2, collection.Count);
            //---------------Execute Test ----------------------
            DMProperty item3 = collection.CreateBusinessObject();
            //---------------Test Result ----------------------
            Assert.AreEqual(3, collection.Count);
            List<DMProperty> list = GetSortedList(collection);
            Assert.AreSame(item2, list[0]);
            Assert.AreEqual(135, item1.OrdinalPosition);
            Assert.AreSame(item1, list[1]);
            Assert.AreEqual(123, item2.OrdinalPosition);
            Assert.AreSame(item3, list[2]);
            Assert.AreEqual(136, item3.OrdinalPosition);
        }

        [Test]
        public void Test_WhenAddExistingItem_WithOrdinal_ShouldNotChangeOrdinal()
        {
            //---------------Set up test pack-------------------
            BusinessObjectCollection<DMProperty> collection = new BusinessObjectCollection<DMProperty>();
            OrderedCollectionController<DMProperty> controller = new OrderedCollectionController<DMProperty>(collection);
            TestUtilsDMProperty.CreateSavedDMProperty();
            TestUtilsDMProperty.CreateSavedDMProperty();
            collection.LoadAll();
            DMProperty property = TestUtilsDMProperty.CreateSavedDMProperty();
            //---------------Assert Precondition----------------
            Assert.AreEqual(2, collection.Count);
            Assert.IsNotNull(property.OrdinalPosition);
            Assert.IsFalse(property.Status.IsDirty);
            //---------------Execute Test ----------------------
            collection.Add(property);
            //---------------Test Result -----------------------
            Assert.AreEqual(3, collection.Count);
            Assert.IsFalse(property.Status.IsDirty);
        }

        [Test]
        public void Test_WhenAddExistingItem_WithNoOrdinal_ShouldNotChangeOrdinal()
        {
            //---------------Set up test pack-------------------
            BusinessObjectCollection<DMProperty> collection = new BusinessObjectCollection<DMProperty>();
            OrderedCollectionController<DMProperty> controller = new OrderedCollectionController<DMProperty>(collection);
            TestUtilsDMProperty.CreateSavedDMProperty();
            TestUtilsDMProperty.CreateSavedDMProperty();
            collection.LoadAll();
            DMProperty property = TestUtilsDMProperty.CreateSavedDMProperty();
            property.OrdinalPosition = 0;
            property.Save();
            //---------------Assert Precondition----------------
            Assert.AreEqual(2, collection.Count);
            Assert.AreEqual(0, property.OrdinalPosition);
            Assert.IsFalse(property.Status.IsDirty);
            //---------------Execute Test ----------------------
            collection.Add(property);
            //---------------Test Result -----------------------
            Assert.AreEqual(3, collection.Count);
            Assert.AreEqual(0, property.OrdinalPosition);
            Assert.IsFalse(property.Status.IsDirty);
        }

        [Test]
        public void Test_WhenAddNewItem_WithOrdinal_ShouldNotChangeOrdinal()
        {
            //---------------Set up test pack-------------------
            BusinessObjectCollection<DMProperty> collection = new BusinessObjectCollection<DMProperty>();
            OrderedCollectionController<DMProperty> controller = new OrderedCollectionController<DMProperty>(collection);
            TestUtilsDMProperty.CreateSavedDMProperty();
            TestUtilsDMProperty.CreateSavedDMProperty();
            collection.LoadAll();
            DMProperty property = TestUtilsDMProperty.CreateSavedDMProperty();
            //---------------Assert Precondition----------------
            Assert.AreEqual(2, collection.Count);
            Assert.IsNotNull(property.OrdinalPosition);
            Assert.IsFalse(property.Status.IsDirty);
            //---------------Execute Test ----------------------
            collection.Add(property);
            //---------------Test Result -----------------------
            Assert.AreEqual(3, collection.Count);
            Assert.IsFalse(property.Status.IsDirty);
        }

        [Test]
        public void Test_WhenAddNewItem_WithNoOrdinal_ShouldSetOrdinalToNext()
        {
            //---------------Set up test pack-------------------
            BusinessObjectCollection<DMProperty> collection = new BusinessObjectCollection<DMProperty>();
            OrderedCollectionController<DMProperty> controller = new OrderedCollectionController<DMProperty>(collection);
            DMProperty property1 = TestUtilsDMProperty.CreateSavedDMProperty();
            DMProperty property2 = TestUtilsDMProperty.CreateSavedDMProperty();
            collection.LoadAll();
            int biggestOrdinal = GetBiggest(property1.OrdinalPosition, property2.OrdinalPosition);
            DMProperty property = new DMProperty();
            //---------------Assert Precondition----------------
            Assert.AreEqual(2, collection.Count);
            Assert.IsNull(property.OrdinalPosition);
            //---------------Execute Test ----------------------
            collection.Add(property);
            //---------------Test Result -----------------------
            Assert.AreEqual(3, collection.Count);
            Assert.AreEqual(biggestOrdinal + 1, property.OrdinalPosition);
        }

        [Test]
        public void Test_GetNextOrdinalPosition_WithEmptyCollection_ShouldReturnOne()
        {
            //---------------Set up test pack-------------------
            BusinessObjectCollection<DMProperty> collection = new BusinessObjectCollection<DMProperty>();
            OrderedCollectionController<DMProperty> controller = new OrderedCollectionController<DMProperty>(collection);
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            int ordinalPosition = controller.GetNextOrdinalPosition();
            //---------------Test Result -----------------------
            Assert.AreEqual(1, ordinalPosition);
        }

        [Test]
        public void Test_GetNextOrdinalPosition_WithLoadedCollection_ShouldReturnOverallMaxPlusOne()
        {
            //---------------Set up test pack-------------------
            BusinessObjectCollection<DMProperty> collection = new BusinessObjectCollection<DMProperty>();
            OrderedCollectionController<DMProperty> controller = new OrderedCollectionController<DMProperty>(collection);
            DMProperty property = TestUtilsDMProperty.CreateSavedDMProperty();
            DMProperty property2 = TestUtilsDMProperty.CreateSavedDMProperty();
            int biggestOrdinal = GetBiggest(property.OrdinalPosition, property2.OrdinalPosition);
            collection.LoadAll();
            //---------------Assert Precondition----------------
            Assert.AreEqual(2, collection.Count);
            //---------------Execute Test ----------------------
            int ordinalPosition = controller.GetNextOrdinalPosition();
            //---------------Test Result -----------------------
            Assert.AreEqual(biggestOrdinal + 1, ordinalPosition);
        }

        private int GetBiggest(int? value1, int? value2)
        {
            int value1Int = value1.GetValueOrDefault(0);
            int value2Int = value2.GetValueOrDefault(0);
            return value1Int > value2Int ? value1Int : value2Int;
        }

        [Test]
        public void Test_GetNextOrdinalPosition_WithRemovedItems_ShouldReturnOverallMaxPlusOne()
        {
            //---------------Set up test pack-------------------
            BusinessObjectCollection<DMProperty> collection = new BusinessObjectCollection<DMProperty>();
            OrderedCollectionController<DMProperty> controller = new OrderedCollectionController<DMProperty>(collection);
            DMProperty property = TestUtilsDMProperty.CreateSavedDMProperty();
            DMProperty property2 = TestUtilsDMProperty.CreateSavedDMProperty();
            int biggestOrdinal = GetBiggest(property.OrdinalPosition, property2.OrdinalPosition);
            collection.LoadAll();
            collection.RemoveAll(dmProperty => true);
            //---------------Assert Precondition----------------
            Assert.AreEqual(0, collection.Count);
            Assert.AreEqual(2, collection.RemovedBusinessObjects.Count);
            //---------------Execute Test ----------------------
            int ordinalPosition = controller.GetNextOrdinalPosition();
            //---------------Test Result -----------------------
            Assert.AreEqual(biggestOrdinal + 1, ordinalPosition);
        }

        [Test]
        public void Test_GetNextOrdinalPosition_WithMarkedForDeleteItems_ShouldReturnOverallMaxPlusOne()
        {
            //---------------Set up test pack-------------------
            BusinessObjectCollection<DMProperty> collection = new BusinessObjectCollection<DMProperty>();
            OrderedCollectionController<DMProperty> controller = new OrderedCollectionController<DMProperty>(collection);
            DMProperty property = TestUtilsDMProperty.CreateSavedDMProperty();
            DMProperty property2 = TestUtilsDMProperty.CreateSavedDMProperty();
            int biggestOrdinal = GetBiggest(property.OrdinalPosition, property2.OrdinalPosition);
            collection.LoadAll();
            collection.ToList().ForEach(dmProperty => dmProperty.MarkForDelete());
            //---------------Assert Precondition----------------
            Assert.AreEqual(0, collection.Count);
            Assert.AreEqual(2, collection.MarkedForDeleteBusinessObjects.Count);
            //---------------Execute Test ----------------------
            int ordinalPosition = controller.GetNextOrdinalPosition();
            //---------------Test Result -----------------------
            Assert.AreEqual(biggestOrdinal + 1, ordinalPosition);
        }

        private List<DMProperty> GetSortedList(BusinessObjectCollection<DMProperty> collection)
        {
            List<DMProperty> list = collection.GetList();
            list.Sort((x, y) => Comparer<int?>.Default.Compare(x.OrdinalPosition, y.OrdinalPosition));
            return list;
        }

        [Test]
        public void Test_MoveUp_WhenLastItem_ShouldMoveUpOrdinal()
        {
            //---------------Set up test pack-------------------
            BusinessObjectCollection<DMProperty> collection = new BusinessObjectCollection<DMProperty>();
            OrderedCollectionController<DMProperty> controller = new OrderedCollectionController<DMProperty>(collection);
            DMProperty prop1 = collection.CreateBusinessObject();
            DMProperty prop2 = collection.CreateBusinessObject();
            DMProperty prop3 = collection.CreateBusinessObject();
            //---------------Assert Preconditions --------------
            AssertPropsAreInOrder(prop1, prop2, prop3);
            //---------------Execute Test ----------------------
            bool result = controller.MoveUp(prop3);
            //---------------Test Result -----------------------
            Assert.IsTrue(result);
            AssertPropsAreInOrder(prop1, prop3, prop2);
        }

        [Test]
        public void Test_MoveUp_WhenMiddleItem_ShouldMoveUpOrdinal()
        {
            //---------------Set up test pack-------------------
            BusinessObjectCollection<DMProperty> collection = new BusinessObjectCollection<DMProperty>();
            OrderedCollectionController<DMProperty> controller = new OrderedCollectionController<DMProperty>(collection);
            DMProperty prop1 = collection.CreateBusinessObject();
            DMProperty prop2 = collection.CreateBusinessObject();
            DMProperty prop3 = collection.CreateBusinessObject();
            //---------------Assert Preconditions --------------
            AssertPropsAreInOrder(prop1, prop2, prop3);
            //---------------Execute Test ----------------------
            bool result = controller.MoveUp(prop2);
            //---------------Test Result -----------------------
            Assert.IsTrue(result);
            AssertPropsAreInOrder(prop2, prop1, prop3);
        }

        [Test]
        public void Test_MoveUp_WhenFirstItem_ShouldNotMove()
        {
            //---------------Set up test pack-------------------
            BusinessObjectCollection<DMProperty> collection = new BusinessObjectCollection<DMProperty>();
            OrderedCollectionController<DMProperty> controller = new OrderedCollectionController<DMProperty>(collection);
            DMProperty prop1 = collection.CreateBusinessObject();
            DMProperty prop2 = collection.CreateBusinessObject();
            DMProperty prop3 = collection.CreateBusinessObject();
            //---------------Assert Preconditions --------------
            AssertPropsAreInOrder(prop1, prop2, prop3);
            //---------------Execute Test ----------------------
            bool result = controller.MoveUp(prop1);
            //---------------Test Result -----------------------
            Assert.IsTrue(result);
            AssertPropsAreInOrder(prop1, prop2, prop3);
        }

        #region Move DMProps

        private static void AssertPropsAreInOrder(DMProperty prop1, DMProperty prop2, DMProperty prop3)
        {
            Assert.AreEqual(1, prop1.OrdinalPosition, "First Prop should be in position 1");
            Assert.AreEqual(2, prop2.OrdinalPosition, "Second Prop should be in position 2");
            Assert.AreEqual(3, prop3.OrdinalPosition, "Third Prop should be in position 2");
        }

        //[Test]
        //public void TestAcceptance_UC8_MoveDMPropertyDown()
        //{
        //    //---------------Set up test pack-------------------
        //    BORegistry.DataAccessor = new DataAccessorInMemory();
        //    DMClass dmClass = TestUtilsDMClass.CreateUnsavedDefaultDMClass();
        //    DMProperty prop1 = dmClass.Properties.CreateBusinessObject();
        //    DMProperty prop2 = dmClass.Properties.CreateBusinessObject();
        //    DMProperty prop3 = dmClass.Properties.CreateBusinessObject();
        //    //---------------Execute Test ----------------------
        //    dmClass.MovePropertyDown(prop1);
        //    //---------------Test Result -----------------------
        //    AssertPropsAreInOrder(prop2, prop1, prop3);
        //}

        ////[Test]
        ////public void TestAcceptance_UC8_MoveDMPropertyDown_SavedProp()
        ////{
        ////    //---------------Set up test pack-------------------
        ////                    //Customised so as to save the state of all the business objects to an appropriate file.
        ////    string db4oDataFile = Path.Combine(@"C:\", "FireStarterModeller.db4o");
        ////    Db4oFactory.Configure().ObjectClass(typeof(BusinessObjectDTO)).CascadeOnUpdate(true);
        ////    DB4ORegistry.DB = Db4oFactory.OpenFile(db4oDataFile);
        ////    BORegistry.DataAccessor = new DataAccessorDB4O(DB4ORegistry.DB);
        ////    DMClass dmClass = TestUtilsDMClass.CreateSavedDMClass();
        ////    DMProperty prop1 = dmClass.Properties.CreateBusinessObject();

        ////    DMProperty prop2 = dmClass.Properties.CreateBusinessObject();
        ////    DMProperty prop3 = dmClass.Properties.CreateBusinessObject();
        ////    TestUtilsShared.UpdateDMPropertyWithValidData(prop1);
        ////    TestUtilsShared.UpdateDMPropertyWithValidData(prop2);
        ////    TestUtilsShared.UpdateDMPropertyWithValidData(prop3);
        ////    dmClass.Save();
        ////    BusinessObjectManager.Instance.ClearLoadedObjects();
        ////    dmClass.Properties.Refresh();
        ////    dmClass.Properties.Sort("OrdinalPosition", true, true);
        ////    DMProperty loadedProperty1 = dmClass.Properties[0];
        ////    DMProperty loadedProperty2 = dmClass.Properties[1];
        ////    DMProperty loadedProperty3 = dmClass.Properties[2];
        ////    //----------------Assert Precondition --------------
        ////    Assert.IsFalse(loadedProperty1.Status.IsNew);
        ////    Assert.AreNotSame(loadedProperty1, prop1);
        ////    Assert.AreEqual(loadedProperty1.PropertyName, prop1.PropertyName);
        ////    //---------------Execute Test ----------------------
        ////    dmClass.MovePropertyDown(loadedProperty1);
        ////    dmClass.MovePropertyDown(loadedProperty1);
        ////    dmClass.MovePropertyDown(loadedProperty1);
        ////    //---------------Test Result -----------------------
        ////    AssertPropsAreInOrder(loadedProperty2, loadedProperty3, loadedProperty1);
        ////}

        //[Test]
        //public void TestAcceptance_UC8_MoveDMPropertyDown_ReordersDMPropertyRelationshipCollection()
        //{
        //    //---------------Set up test pack-------------------
        //    BORegistry.DataAccessor = new DataAccessorInMemory();
        //    DMClass dmClass = TestUtilsDMClass.CreateUnsavedDefaultDMClass();
        //    DMProperty prop1 = dmClass.Properties.CreateBusinessObject();
        //    DMProperty prop2 = dmClass.Properties.CreateBusinessObject();
        //    DMProperty prop3 = dmClass.Properties.CreateBusinessObject();
        //    //---------------Execute Test ----------------------
        //    dmClass.MovePropertyDown(prop1);
        //    BusinessObjectCollection<DMProperty> props = dmClass.Properties;
        //    props.Sort("OrdinalPosition", true, true);
        //    //---------------Test Result -----------------------
        //    Assert.AreSame(prop2, props[0]);
        //    Assert.AreSame(prop1, props[1]);
        //    Assert.AreSame(prop3, props[2]);
        //}

        //[Test]
        //public void Test_MoveDMPropertyDown_PropNull_DoesNothing()
        //{
        //    //---------------Set up test pack-------------------
        //    BORegistry.DataAccessor = new DataAccessorInMemory();
        //    DMClass dmClass = TestUtilsDMClass.CreateUnsavedDefaultDMClass();
        //    DMProperty prop1 = dmClass.Properties.CreateBusinessObject();
        //    DMProperty prop2 = dmClass.Properties.CreateBusinessObject();
        //    DMProperty prop3 = dmClass.Properties.CreateBusinessObject();
        //    //---------------Execute Test ----------------------
        //    dmClass.MovePropertyDown(null);
        //    BusinessObjectCollection<DMProperty> props = dmClass.Properties;
        //    props.Sort("OrdinalPosition", true, true);
        //    //---------------Test Result -----------------------
        //    Assert.AreSame(prop1, props[0]);
        //    Assert.AreSame(prop2, props[1]);
        //    Assert.AreSame(prop3, props[2]);
        //}

        //[Test]
        //public void Test_MoveDMPropertyUp_PropNull_DoesNothing()
        //{
        //    //---------------Set up test pack-------------------
        //    BORegistry.DataAccessor = new DataAccessorInMemory();
        //    DMClass dmClass = TestUtilsDMClass.CreateUnsavedDefaultDMClass();
        //    DMProperty prop1 = dmClass.Properties.CreateBusinessObject();
        //    DMProperty prop2 = dmClass.Properties.CreateBusinessObject();
        //    DMProperty prop3 = dmClass.Properties.CreateBusinessObject();
        //    //---------------Execute Test ----------------------
        //    dmClass.MovePropertyUp(null);
        //    BusinessObjectCollection<DMProperty> props = dmClass.Properties;
        //    props.Sort("OrdinalPosition", true, true);
        //    //---------------Test Result -----------------------
        //    Assert.AreSame(prop1, props[0]);
        //    Assert.AreSame(prop2, props[1]);
        //    Assert.AreSame(prop3, props[2]);
        //}

        //[Test]
        //public void TestCreateDMPropertyOrdinalPosition_SaveAndAddOne_ShouldHaveNextNumber()
        //{
        //    //---------------Set up test pack-------------------
        //    BORegistry.DataAccessor = new DataAccessorInMemory();
        //    DMClass dmClass = TestUtilsDMClass.CreateUnsavedValidDMClass();
        //    DMProperty dmProp1 = TestUtilsDMProperty.SetDMPropTOValidValues(dmClass.CreateDMProp());
        //    DMProperty dmProp2 = TestUtilsDMProperty.SetDMPropTOValidValues(dmClass.CreateDMProp());
        //    dmClass.CreateObjectIdentity(dmProp1);
        //    dmClass.Save();
        //    //---------------Assert Preconditions--------------
        //    Assert.AreEqual(1, dmProp1.OrdinalPosition);
        //    Assert.AreEqual(2, dmProp2.OrdinalPosition);
        //    Assert.IsFalse(dmProp2.Status.IsNew);
        //    //---------------Execute Test ----------------------
        //    DMProperty dmProp3 = dmClass.CreateDMProp();
        //    //---------------Test Result -----------------------
        //    Assert.AreEqual(3, dmProp3.OrdinalPosition);
        //    Assert.IsTrue(dmProp3.Status.IsNew);
        //}

        //[Test]
        //public void TestCreateDMPropertyOrdinalPosition_SaveAndDeleteFirstAddOne_ShouldHaveNextNumber()
        //{
        //    //---------------Set up test pack-------------------
        //    BORegistry.DataAccessor = new DataAccessorInMemory();
        //    DMClass dmClass = TestUtilsDMClass.CreateUnsavedValidDMClass();
        //    DMProperty dmProp1 = TestUtilsDMProperty.SetDMPropTOValidValues(dmClass.CreateDMProp());
        //    DMProperty dmProp2 = TestUtilsDMProperty.SetDMPropTOValidValues(dmClass.CreateDMProp());
        //    dmClass.CreateObjectIdentity(dmProp2);
        //    dmClass.Save();
        //    //---------------Assert Preconditions--------------
        //    Assert.AreEqual(1, dmProp1.OrdinalPosition);
        //    Assert.AreEqual(2, dmProp2.OrdinalPosition);
        //    Assert.IsFalse(dmProp2.Status.IsNew);
        //    //---------------Execute Test ----------------------
        //    dmProp1.MarkForDelete();
        //    DMProperty dmProp3 = dmClass.CreateDMProp();
        //    //---------------Test Result -----------------------
        //    Assert.AreEqual(3, dmProp3.OrdinalPosition);
        //    Assert.IsTrue(dmProp3.Status.IsNew);
        //}


        //[Test]
        //public void TestCreate_Serialise_Desrialise_DMPropertyOrdinalPositionIsSet()
        //{
        //    //---------------Set up test pack-------------------
        //    BORegistry.DataAccessor = new DataAccessorInMemory();
        //    DMClass dmClass = TestUtilsDMClass.CreateUnsavedDefaultDMClass();
        //    IFormatter formatter = new BinaryFormatter();
        //    MemoryStream memoryStream = new MemoryStream();
        //    formatter.Serialize(memoryStream, dmClass);
        //    memoryStream.Seek(0, SeekOrigin.Begin);
        //    BusinessObjectManager.Instance.ClearLoadedObjects();
        //    dmClass = (DMClass)formatter.Deserialize(memoryStream);
        //    //---------------Execute Test ----------------------
        //    DMProperty dmProp1 = dmClass.CreateDMProp();
        //    DMProperty dmProp2 = dmClass.CreateDMProp();
        //    //---------------Test Result -----------------------
        //    Assert.AreEqual(1, dmProp1.OrdinalPosition);
        //    Assert.AreEqual(2, dmProp2.OrdinalPosition);
        //}

        //[Test]
        //public void TestCreateDMProperty_ViaRelationship_OrdinalPosition_NextAvailable()
        //{
        //    //---------------Set up test pack-------------------
        //    BORegistry.DataAccessor = new DataAccessorInMemory();
        //    DMClass dmClass = TestUtilsDMClass.CreateUnsavedDefaultDMClass();
        //    //---------------Execute Test ----------------------
        //    DMProperty dmProp1 = dmClass.Properties.CreateBusinessObject();
        //    DMProperty dmProp2 = dmClass.Properties.CreateBusinessObject();
        //    //---------------Test Result -----------------------
        //    Assert.AreEqual(1, dmProp1.OrdinalPosition);
        //    Assert.AreEqual(2, dmProp2.OrdinalPosition);
        //}

        #endregion //Move DMProps
    }
}